<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/1
 * Time: 19:08
 */

namespace app\api\validate;


use app\lib\Exception\ParameterException;

class NewOrder extends BaseValidate
{
    protected $rule =[
        'books'=>'require|checkBooks',
        'type' => 'require|checkType'
    ];

    protected $bookRule = [
        'book_id'=>'require|isPositiveInteger',
        'count'=>'require|isPositiveInteger'
    ];

    protected function checkBooks($values){
        if(empty($values)){
            throw new ParameterException([
                'info'=>'商品列表不能为空'
            ]);
        }

        if(!is_array($values)){
            throw new ParameterException([
                'info'=>'商品列表参数错误'
            ]);
        }

        foreach ($values as $book){
            $this->checkBook($book);
        }

        return true;
    }

    protected function checkBook($book){
        $validate = new BaseValidate($this->bookRule);
        $result = $validate->check($book);
        if(!$result){
            throw new ParameterException([
                'info'=>'商品列表参数错误'
            ]);
        }
    }
}